$(function() {
	$('#unReadMyStationletter').click(function() {
		fsJson.url = webroot + '/stationletter/json/findReceStationletter.shtml';
		fsJson.type = 'rece';
		fsJson.data = {
				issee : 0
		};
		findStationletter(1);
		refreshBtn('unReadMyStationletter');
	});

	$('#receMyStationletter').click(function() {
		fsJson.url = webroot + '/stationletter/json/findReceStationletter.shtml';
		fsJson.type = 'rece';
		fsJson.data = {};
		findStationletter(1);
		refreshBtn('receMyStationletter');
	});

	$('#sendMyStationletter').click(function() {
		fsJson.url = webroot + '/stationletter/json/findSendStationletter.shtml';
		fsJson.type = 'send';
		fsJson.data = {};
		findStationletter(1);
		refreshBtn('sendMyStationletter');
	});

	//刷新操作按钮
	function refreshBtn(_id) {
		$.each($(".myStationletter"), function(i, obj) {
			_obj = $(obj);
			if(_obj.attr('id') === $('#' + _id).attr('id')) {
				_obj.attr("class", "myStationletter btnorangebig_hover");
			}
			else {
				_obj.attr("class", "myStationletter btnorangebig");
			}
		});
	}

	$('#unReadMyStationletter').click();
});

var fsJson = {};

var findStationletterPage = new Page("findStationletterPage", findStationletter, "bodyInfo", "bodyInfoPage");
findStationletterPage.size = 5;
findStationletterPage.pageSizeArr = [5, 10, 15];
//设置开始字符串
findStationletterPage.beginString = '<table><thead><tr>'
							+'<th>状态</th>'
							+'<th>标题</th>'
							+'<th>接收人</th>'
							+'<th>发送人</th>'
							+'<th>时间</th>'
							+'<th>操作</th></tr></thead><tbody>';
//设置结束字符串
findStationletterPage.endString = "</tbody></table>";
/**
 * 查看站内信
 */
function findStationletter(_page) {
	if(_page != undefined) {
		findStationletterPage.page = _page;
	}
	var _data = {
			"page" : findStationletterPage.page,
			"size" : findStationletterPage.size
		};
	if(fsJson.data.issee != undefined) {
		_data.issee = fsJson.data.issee;
	}
	jQuery.ajax({
		url : fsJson.url,
		data : _data,
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		findStationletterPage.pageBeforeSend('<tr><td colspan="6">加载站内信中...</td></tr>');
		loading("加载邀请信息中...");
	},
	error : function(json){
		findStationletterPage.pageError('<tr><td colspan="6">加载站内信出错了~</td></tr>');
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var _status;
				var _operation;
				if(fsJson.type === 'rece') {
					_status = (obj.issee === 0 ? '<a class="btngreen" href="javascript:;" onclick="myStler.findDetailStationletter(' + obj.id + ', \'findStationletter(1)\');">未看</a>' : '已看');
					_operation = '<a href="javascript:;" onclick="myStler.findDetailStationletter(' + obj.id + ', \'findStationletter(1)\');">查看</a>';
				} else {
					_status = '未知';
					_operation = '<a href="javascript:;" onclick="myStler.findDetailStationletter(' + obj.id + ');">查看</a>';
				}
				var result = '<tr><td>' + _status + '</td>'
							+ '<td>' + obj.title + '</td><td>' + obj.recenickname + '</td>'
							+ '<td>' + obj.sendnickname + '</td><td>' + dateUtil.getFormatDate(new Date(obj.addtime)) + '</td>'
							+ '<td>' + _operation + '</td></tr>';
				return result;
			}
			findStationletterPage.pageOperate(json.data, { resultFn : getResult, dataNull : '<tr><td colspan="6">没有查找到站内信的数据!</td></tr>', pageFootStyle : findStationletterPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}

var myStler = {
		/**
		 * 查看站内信
		 * @param _id		编号
		 * @param _callback	执行成功后的回调函数
		 */
		findDetailStationletter : function(_id, _callback) {
			jQuery.ajax({
				url : webroot + "/stationletter/json/lockStationletter.shtml",
				data : {"id" : _id},
				type : "POST",
				dataType : "json",
				async : true,
				beforeSend: function(){
					//提交处理的函数
					loading("努力加载站内信详情中...");
				},
				error : function(json){
					alert("加载站内信详情出错了~");
					unloading();
				},
				//返回的是一个对象
				success : function(json){
					if(json.result === "success") {
						var _content = '<div class="promptsys">';
						var _obj = json.data;
						_content += '<p><label class="bold">标题:</label> ' + _obj.title + '</p>';
						_content += '<p><label class="bold">内容:</label> ' + _obj.content + '</p>';
						_content += '<p><label class="bold">接收人:</label> ' + _obj.recenickname + '</p>';
						_content += '<p><label class="bold">发送时间:</label> ' + dateUtil.getFormatDate(new Date(_obj.addtime), 'yyyy年MM月dd日 HH:mm:ss') + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
									+ '<label class="bold">发送人:</label> ' + _obj.sendnickname + '</p>';
						_content += '</div>';
						prompt.colorbox(_content, '查看站内信详情');

						if(_callback != undefined) {
							eval(_callback);
						}

						//重新加载未读信息
						//mainCenter.unReadInfo();
					}
					else if(json.result === "error_not_login") {
						alert("您还没有登录, 请您登录后再来操作吧~");
					}
					else if(json.result === 'error') {
						alert('请求操作, 系统出现异常!');
					}
					unloading();
				}
			});
		}
};